package o2o.web.actions.sys;

import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.JYPageUtils;
import com.jinyou.utils.common.ObjectUtils;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.shop.ShopMemory;
import o2o.cache.sys.CityAdminShopRelationMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.company.model.ShopInfo;
import o2o.dao.PO.sys.CityAdminShopRelation;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.VO.shop.ShopInfoVO;
import o2o.task.image.ImageService;
import o2o.web.actions.user.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname CityAdminShopRelationAction
 * @Description TODO
 * @Date 2019/4/23 15:50
 * @Created by yangtk
 */
@Controller
public class CityAdminShopRelationAction {

    // 添加或者修改 店铺的关系
    @RequestMapping(value = "/city/shop/relation/add-update", method = RequestMethod.POST)
    @ResponseBody
    public String add_city_Admin_relation(String token, Long shopId, Long cityAdminId) {
        UserInfo userInfo = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (userInfo == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (!User.hasAdminAuth(userInfo.getUserType())) {
            return AjaxResult.errorResult("您没有权限操作");
        }
        if (ValidateUtil.isNotID(shopId)) {
            return AjaxResult.errorResult("店铺id不能为空");
        }
        if (ValidateUtil.isNotID(cityAdminId)) {
            return AjaxResult.errorResult("代理id不能为空");
        }

        return AjaxResult.successResult();
    }

    // 获取指定代理下的所有的店铺信息
    @RequestMapping(value = "/city/shop/relation/list", method = RequestMethod.POST)
    @ResponseBody
    public String list_of_shop_by_cityAdminId(String token, Long cityAdminId, Integer page, Integer size) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (!User.hasAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有权限操作");
        }
        if (ValidateUtil.isNotID(cityAdminId)) {
            return AjaxResult.errorResult("城市代理id不能为空");
        }
        if (!ValidateUtil.isAbsInteger(page)) {
            page = 1;
        }
        if (!ValidateUtil.isAbsInteger(size)) {
            size = 20;
        }
        List<CityAdminShopRelation> shopRelationList = CityAdminShopRelationMemory.getInstance().listOfCityAdminId(cityAdminId);
        if (ValidateUtil.isAbsList(shopRelationList)) {
            List<ShopInfo> shopInfoList = new ArrayList<>();
            for (CityAdminShopRelation c : shopRelationList) {
                if (c == null) {
                    continue;
                }
                if (ValidateUtil.isNotID(c.getShopId())) {
                    continue;
                }
                ShopInfo shopInfo = ShopMemory.getInstance().getShopById(c.getShopId());
                if (shopInfo == null) {
                    continue;
                }
                shopInfoList.add(shopInfo);
            }
            if (ValidateUtil.isAbsList(shopInfoList)) {
                JYPageUtils.PageList pageList = JYPageUtils.getList(shopInfoList, page, size);
                List<ShopInfoVO> list = new ArrayList<>();
                for (ShopInfo shopInfo : (List<ShopInfo>) pageList.list) {
                    ShopInfoVO shopInfoVO = new ShopInfoVO();
                    try {
                        ObjectUtils.copyPropertiesExclude(shopInfo, shopInfoVO);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (shopInfoVO == null) {
                        continue;
                    }
                    if (ValidateUtil.isNotNull(shopInfoVO.getImageUrl()) && ValidateUtil.isNotNetUrl(shopInfoVO.getImageUrl())) {
                        String thumb = ImageService.getThumbnailImgUrl(shopInfoVO.getImageUrl(), shopInfo.getImgUpVer(), shopInfo.getThumbnailState());
                        shopInfoVO.setImageThumbnailUrl(thumb);
//                        shopInfoVO.setImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + File.separator + shopInfoVO.getImageUrl());
                        shopInfoVO.setImageUrl(ImageService.getImgUrl(shopInfo.getImgUpVer()) + File.separator + shopInfoVO.getImageUrl());
                    }
                    list.add(shopInfoVO);
                }
                return AjaxResult.successResult(pageList.totalCount, list.size(), list);
            }
        }
        return AjaxResult.successResult(0, 0, null);
    }


}
